package com.chidopi.market.aa.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.aa.domain.FunctionItems;


public class FunctionItemDaoImpl extends AbstractBaseDaoImpl<FunctionItems> {
	private static Logger logger = LoggerFactory.getLogger(FunctionItemDaoImpl.class);
	public List<FunctionItems> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select * from functionitems ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select * from functionitems where " + sqlWhere;
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
		}
	}

	public FunctionItems findById(Object id) {
		String sql = "id=?";
		List<FunctionItems> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	public int insert(FunctionItems t) {
		String sql = "insert into functionitems (ID, ALIAS, ITEM_NAME, ENABLE_FLAG, FUNCTION_ID) "
				+ "values (:ID, :ALIAS, :ITEM_NAME, :ENABLE_FLAG, :FUNCTION_ID)";
		return super.execSQL(sql, t);
	}

	public int update(FunctionItems t) {
		String sql = "update functionitems set ALIAS = :ALIAS, ENABLE_FLAG = :ENABLE_FLAG, ITEM_NAME = :ITEM_NAME, FUNCTION_ID = :FUNCTION_ID where ID= :ID";
		return super.execSQL(sql, t);
	}

	public int delete(FunctionItems t) {
		String sql = "delete from functionitems where ID :ID)";
		return super.execSQL(sql, t);
	}

	@Override
	public String getSequenceName() {
		return "FunctionItems_SEQ";
	}

	@Override
	public String getTable() {
		return "FunctionItems";
	}

	@Override
	protected RowMapper<FunctionItems> getRowMapper() {
		return ParameterizedBeanPropertyRowMapper.newInstance(FunctionItems.class);
	}
}
